<?php
use yii\helpers\Html;
$post=\Yii::$app->session->getFlash('post');
?>
<?php $this->beginBlock('title'); ?>
    <?php if(isset($model['type_id'])):?>编辑<?php else:?>新增<?php endif;?>游戏类型
<?php $this->endBlock();?>
<?php $this->beginBlock('breadcrumb'); ?>
<li><a href="<?php echo \Yii::$app->urlManager->createUrl('/game-type/index')?>">游戏类型</a></li>
<li class="active"><a href="<?php echo \Yii::$app->urlManager->createUrl('/game-type/edit',['type_id'=>isset($model['type_id'])?$model['type_id']:0]);?>"><?php if(isset($model['type_id'])):?>编辑<?php else:?>新增<?php endif;?>游戏类型</a></li>
<?php $this->endBlock();?>

<link rel="stylesheet" type="text/css" href="<?php echo Yii::$app->request->baseUrl; ?>/js/bootstrap-fileinput-master/css/fileinput.css" />
<style>
.show_upload { display:none; }
</style>
<div class="row">
	<div class="col-sm-12">
		<section class="panel">
			<div class="panel-body">
				<div class="form">
					<form class="cmxform form-horizontal tasi-form" method="post" action="<?php echo \Yii::$app->urlManager->createUrl('/game-type/save');?>">
						<div class="form-group">
							<label for="type_name" class="control-label col-lg-2">名称</label>
							<div class="col-lg-10">
                               	<input class="form-control" id="type_name" name="type_name" value="<?php if(isset($post['type_name'])){ echo $post['type_name'];}else{ echo Html::encode(isset($model['type_name'])?$model['type_name']:''); }?>" type="text" required />
                            </div>
						</div>
                        <div class="form-group">
                            <label for="alias" class="control-label col-lg-2">别名</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="alias" type="text" value="<?php if(isset($post['alias'])){ echo $post['alias'];}else{ echo Html::encode(isset($model['alias'])?$model['alias']:''); }?>" name="alias" required />
                            </div>
                        </div>
                        <?php if(isset($model['type_id'])):?>
                        <div class="form-group">
                            <label for="game_count" class="control-label col-lg-2">游戏数量</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="game_count" type="text" disabled="" value="<?php if(isset($post['game_count'])){ echo $post['game_count'];}else{ echo Html::encode(isset($model['game_count'])?$model['game_count']:''); }?>" name="game_count" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="control-label col-lg-2">上级类型</label>
                            <div class="col-lg-10">
                                <p class="form-control-static"><?php if(isset($parent_node)):?><?=$parent_node['type_name'];?><?php else:?>一级类型<?php endif;?>
                                 [<a href="javascript:;" id="editBelongType" data-id="<?=$model['type_id']?>">修改</a>]</p>
                            </div>
                        </div>
                        <?php endif;?>
                        <div class="form-group"<?php if(isset($model['type_id'])):?> style="display: none;"<?php endif;?> id="selType">
                            <label for="path" class="control-label col-lg-2">上级类型</label>
                            <div class="col-lg-10">
                                <select class="form-control sub_type" style="width:30%;display:inline;" name="path[]">
                                	<option value="0" selected>--做为第一级类型--</option>
									<?php foreach ($types as $type):?>
					                	<option value="<?=$type['type_id'];?>"><?=$type['type_name'];?></option>
					                <?php endforeach;?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="number" class="control-label col-lg-2">图片</label>
                            <div class="col-lg-10">
                            	<?php if(isset($model['pic'])):?>
	                            	<span class="file-input">
	                            		<div class="file-preview">
	                            			<div class="close fileinput-remove">×</div>
	                            			<div class="">
	                            				<div class="file-preview-thumbnails">
													<div data-fileindex="0" class="file-preview-frame">
	   													<img style="width:auto;height:160px;" alt="<?php echo isset($model['type_name'])?$model['type_name']:'';?>" title="<?php echo isset($model['type_name'])?$model['type_name']:'';?>" class="file-preview-image" src="<?=filepath($model['pic']);?>">
	   													<div class="file-thumbnail-footer">
	    													<div style="width: 170px;" class="file-caption-name" title="<?php echo isset($model['type_name'])?$model['type_name']:'';?>"><?php echo isset($model['type_name'])?$model['type_name']:'';?></div>
														</div>
													</div>
												</div>
												<div class="clearfix"></div>
											</div>
										</div>
									</span>
									<div class="show_upload">
										<input id="file_pic" class="file" type="file" name="file" multiple=true>
			                        	<span class="help-block">请上传[jpg、png、gif]格式的图片，建议规格为500*500，不大于1M </span>
									</div>
                            	<?php else:?>
                            		<input id="file_pic" class="file" type="file" name="file" multiple=true>
		                        	<span class="help-block">请上传[jpg、png、gif]格式的图片，建议规格为500*500，不大于1M </span>
		                        <?php endif;?>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-lg-offset-2 col-lg-10">
                                <input type="hidden" name="_csrf" id="_csrf" value="<?=\Yii::$app->request->csrfToken; ?>" />
        						<?php if(isset($model['type_id'])):?><input type="hidden" name="type_id" value="<?=$model['type_id'];?>" /><?php endif;?>
        						<input type="hidden" value="<?php echo isset($model['pic'])?$model['pic']:'';?>" id="pic" name="pic" />
                                <button class="btn btn-success" type="submit"> 保存</button>
                                <button class="btn btn-default" type="button" onclick="javascript:history.go(-1)"> 取消</button>
                            </div>
                        </div>
                    </form>
                </div>
			</div>
		</section>
	</div>
</div>

<?php
$this->beginBlock('script');
?>
<script type="text/javascript" src="<?php echo Yii::$app->request->baseUrl; ?>/js/bootstrap-fileinput-master/js/fileinput.js"></script>
<script type="text/javascript">
//约束上传图片条件
$("#file_pic").fileinput({
    allowedFileExtensions : ['jpg', 'png','gif'],
    showPreview : true,
    maxFileSize: 1000,
    minImageWidth: 500,
    minImageHeight: 500,
    showUpload:false,
    //maxFilesNum: 1,
});

$(function() {
	//开始上传图片
    $(document).on('change', "#file_pic", function() {
    	var csrfToken = $('meta[name="csrf-token"]').attr("content");
        $.ajaxFileUpload({
            url : "<?php echo \Yii::$app->urlManager->createUrl('/storage/upload')?>",
            secureuri : false,
            fileElementId : 'file_pic',
            dataType : 'json',
            data : { _csrf:csrfToken },
            success : function (data, status) {
                $("#pic").val(data.path);
            },
            error : function (data, status, e) {
                alert(e);
            }
        });
    });
});

//修改图片时，关闭图片显示改为上传
$(".fileinput-remove").click(function(){
	$(".file-input").toggle();
	$("#pic").val("");
	$(".show_upload").show();
	$(".file-input-new").show();
});

//点击修改类型
$(document).ready(function(){
    $("#editBelongType").click(function() {
        $("#selType").toggle();
        if ($("#selType").is(":hidden")) {
            $(this).text('修改');
        } else {
            $(this).text('取消修改');
        }
    });
});

//展开下级类型
$(document).on('change', ".sub_type", function() {
    var parent_id = $(this).val();
    var obj = $(this);
    obj.nextAll().remove();
    $.getJSON("<?=\Yii::$app->urlManager->createUrl('game-type/sub-types');?>", { parent_id:parent_id }, function(data) {
        if (data.length > 0) {
            var select = '<select class="form-control sub_type" style="width:30%;display:inline;" name="path[]" tabindex="1"><option value="">--请选择--</option>';
            $(data).each(function(i, e) {
                select += "<option value='"+ e.type_id+"'>"+ e.type_name+"</option> ";
            });
            select += "</select>";
            obj.parent().append(select);
        }
    });
});
</script>
<?php $this->endBlock('script');?>